/*会议周日历控件*/
/**
 * 设置日历导航日期
 */
(function ($) {
    $.fn.extend({
        setNavigateDate: function (options) {
            var settings = $.extend({
                startTime: null,
                endTime: null
            }, options);
            return this.each(function () {
                _this = $(this);
                var date1 = formatStringToDate(settings.startTime, 0);
                var date2 = formatStringToDate(settings.endTime, 0);
                _this.find("#selecteDatetimeTable").find("tr").find("td").eq(2).text(formatDateToStringCN(date1) + "星期一");
                _this.find("#selecteDatetimeTable").find("tr").find("td").eq(3).text(formatDateToStringCN(date2) + "星期日");
                _this.find("#btnPrev").click(function () {
                    var date1 = formatStringToDate(settings.startTime, -7);
                    var date2 = formatStringToDate(settings.endTime, -7);
                    settings.startTime = formatDateToStringEN(date1) + " 00:00:00";
                    settings.endTime = formatDateToStringEN(date2) + " 23:59:59";
                    _this.find("#selecteDatetimeTable").find("tr").find("td").eq(2).text(formatDateToStringCN(date1) + "星期一");
                    _this.find("#selecteDatetimeTable").find("tr").find("td").eq(3).text(formatDateToStringCN(date2) + "星期日");
                    _this.trigger("selectPrevDate", [settings.startTime, settings.endTime]);
                });
                _this.find("#btnNext").click(function () {
                    var date1 = formatStringToDate(settings.startTime, 7);
                    var date2 = formatStringToDate(settings.endTime, 7);
                    settings.startTime = formatDateToStringEN(date1) + " 00:00:00";
                    settings.endTime = formatDateToStringEN(date2) + " 23:59:59";
                    _this.find("#selecteDatetimeTable").find("tr").find("td").eq(2).text(formatDateToStringCN(date1) + "星期一");
                    _this.find("#selecteDatetimeTable").find("tr").find("td").eq(3).text(formatDateToStringCN(date2) + "星期日");
                    _this.trigger("selectNextDate", [settings.startTime, settings.endTime]);
                });

                function formatDateToStringEN(dt) {
                    var strYear = dt.getFullYear();
                    var strMonth = (dt.getMonth() + 1);
                    if (strMonth < 10) {
                        strMonth = "0" + strMonth;
                    }
                    var strDay = dt.getDate();
                    if (strDay < 10) {
                        strDay = "0" + strDay;
                    }
                    return strYear + "-" + strMonth + "-" + strDay;
                }

                function formatDateToStringCN(dt) {
                    var strYear = dt.getFullYear();
                    var strMonth = (dt.getMonth() + 1);
                    if (strMonth < 10) {
                        strMonth = "0" + strMonth;
                    }
                    var strDay = dt.getDate();
                    if (strDay < 10) {
                        strDay = "0" + strDay;
                    }
                    return strYear + "年" + strMonth + "月" + strDay + "日";
                }

                function formatStringToDate(dt, interval) {
                    var date = new Date(dt.replace(/-/ig, '/'));
                    date.setDate(date.getDate() + interval);
                    return date;
                }
            });
        },
        drewWeekCalendar: function (options) {
            var settings = $.extend({
                data: null
            }, options);
            return this.each(function () {
                _this = $(this);
                var data = settings.data;
                _this.find("#meetingCalendar").find("td").removeClass("calendar-book").removeClass("calendar-approval");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-important-book").removeClass("calendar-important-approval");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-cancel").removeClass("calendar-finish");
                _this.find("#meetingCalendar").find("td").removeClass("calendar-accept").removeClass("calendar-selected").removeClass("calendar-booking");
                for (var i = 0; i < data.length; i++) {
                    var starttime = new Date(data[i].startTime.replace(/-/ig, '/'));
                    var day = starttime.getDay();
                    if (day == 0) {
                        day = 7;
                    }
                    var endtime = new Date(data[i].endTime.replace(/-/ig, '/'));
                    var td = (starttime.getHours() - 7) * 4 + starttime.getMinutes() / 15;
                    switch (data[i].status) {
                        case "0":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-booking");
                            }
                            break;
                        case "1":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-book");
                            }
                            break;
                        case "2":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-approval");
                            }
                            break;
                        case "3":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-important-book");
                            }
                            break;
                        case "4":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-important-approval");
                            }
                            break;
                        case "5":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-cancel");
                            }
                            break;
                        case "6":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-finish");
                            }
                            break;
                        case "7":
                            for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                                _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-accept");
                            }
                            break;
                        default:
                            break;
                    }
                }
            });
        },
        selectedConference: function (options) {
            var settings = $.extend({
                data: null
            }, options);
            return this.each(function () {
                _this = $(this);
                var data = settings.data;
                if (data != null) {
                    var starttime = new Date(data.startTime.replace(/-/ig, '/'));
                    var day = starttime.getDay();
                    if (day == 0) {
                        day = 7;
                    }
                    var endtime = new Date(data.endTime.replace(/-/ig, '/'));
                    var td = (starttime.getHours() - 7) * 4 + starttime.getMinutes() / 15;
                    _this.find("#meetingCalendar").find("td").removeClass("calendar-selected");
                    for (var a = 1; a <= (endtime - starttime) / 1000 / 60 / 15; a++) {
                        _this.find("#meetingCalendar").find("tr").eq(day).find("td").eq(td + a).addClass("calendar-selected");
                    }
                }
            });
        }
    });
})(jQuery);